package test5.lupeng.jf.com.teamdemo.activitys;

import android.content.Intent;
import android.graphics.Color;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ScrollView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.google.gson.Gson;

import java.io.IOException;
import java.util.ArrayList;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
import test5.lupeng.jf.com.teamdemo.R;
import test5.lupeng.jf.com.teamdemo.adapter.BlogCommentAdapter;
import test5.lupeng.jf.com.teamdemo.bean.request.BlogCommentBean;
import test5.lupeng.jf.com.teamdemo.bean.response.BlogCommentResponse;
import test5.lupeng.jf.com.teamdemo.bean.response.BlogResponse;
import test5.lupeng.jf.com.teamdemo.untils.HTTPUtils;

public class BlogDetailsActivity extends AppCompatActivity {
    private static final String TAG = "BlogDetailsActivity";
    ArrayList<BlogCommentResponse.ResultBean> mResultBeen = new ArrayList<>();
    private BlogResponse.ResultBean bean;
    private BlogCommentAdapter mAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_blog_details);
        bean = getIntent().getParcelableExtra("blog");
        Log.e(TAG, "onCreate: " + bean.toString());
        Log.e(TAG, "onCreate: "+bean.getContentArr().toString() );
        initView();
        getData(bean.getBlogId(), 5, 0);
    }

    /**
     * 初始化视图
     */
    private void initView() {
        ImageView iv_back = (ImageView) findViewById(R.id.iv_back);
        iv_back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });
        ImageView iv_headImage = (ImageView) findViewById(R.id.iv_headImage);
        Glide.with(this).load(bean.getAuthor().getHeadProtrait()).asBitmap()
                .into(iv_headImage);


        TextView tv_userName = (TextView) findViewById(R.id.tv_userName);
        tv_userName.setText(bean.getAuthor().getUsername());

        TextView tv_title = (TextView) findViewById(R.id.tv_title);
        tv_title.setText(bean.getTitle());


        TextView tv_likeSize = (TextView) findViewById(R.id.tv_likeSize);
        tv_likeSize.setText(bean.getLikeSize() + "");

        TextView tv_commentsize = (TextView) findViewById(R.id.tv_commentsize);
        tv_commentsize.setText(bean.getCommentsSize() + "");


        //加载评论内容
        LinearLayout ll_content = (LinearLayout) findViewById(R.id.ll_content);
        for (int i = 0; i < bean.getContentArr().size(); i++) {
            String url = bean.getContentArr().get(i);
            if (url.endsWith(".jpg") || url.endsWith(".png")) {
                ImageView imageView = new ImageView(this);
                LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT
                        , ViewGroup.LayoutParams.WRAP_CONTENT);
                params.topMargin = 5;
                params.gravity = Gravity.CENTER;
                imageView.setLayoutParams(params);

                Glide.with(this).load(url).into(imageView);
                ll_content.addView(imageView);

            } else {
                TextView textView = new TextView(this);
                LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,
                        ViewGroup.LayoutParams.WRAP_CONTENT);
                params.topMargin = 5;
                params.gravity = Gravity.CENTER;
                textView.setTextColor(Color.BLACK);
                textView.setTextSize(22);
                textView.setLayoutParams(params);
                textView.setText(url);
                ll_content.addView(textView);
            }
        }

        final TextView tv_size = (TextView) findViewById(R.id.tv_size);
        tv_size.setText(bean.getCommentsSize() + "个评论");


        //显示评论内容
        RecyclerView rl_blogcomment = (RecyclerView) findViewById(R.id.rl_blogcomment);
        rl_blogcomment.setLayoutManager(new LinearLayoutManager(this));
        mAdapter = new BlogCommentAdapter(mResultBeen);
        rl_blogcomment.setAdapter(mAdapter);



        //查看更多
        Button bt_more= (Button) findViewById(R.id.bt_more);
        bt_more.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                Intent intent  =new Intent(BlogDetailsActivity.this,MoreBlogCommentActivity.class);
                intent.putExtra("id_blog",bean.getBlogId());

                startActivity(intent);
            }
        });


        //发表评论
       TextView tv_publish= (TextView) findViewById(R.id.tv_publish);
        tv_publish.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                 Intent intent  = new Intent(BlogDetailsActivity.this,PublishCommentActivity.class);
                intent.putExtra("articleId",bean.getBlogId());
                 startActivity(intent);

            }
        });

        final ScrollView scrollView= (ScrollView) findViewById(R.id.scrollView);
         final LinearLayout ll_toolbar= (LinearLayout) findViewById(R.id.ll_toolbar);
       ImageView iv_location= (ImageView) findViewById(R.id.iv_location);
        iv_location.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                int[]  location  =new int[2];
                Log.e(TAG, "onClick: "+location[0]+"      " +location[1]);
                tv_size.getLocationOnScreen(location);

                Log.e(TAG, "onClick: "+ scrollView.getMeasuredHeight());
                int offsize=location[1]-ll_toolbar.getMeasuredHeight();
                scrollView.scrollTo(0,offsize);

            }
        });

    }

    @Override
    protected void onResume() {
        super.onResume();
        getData(bean.getBlogId(),5,0);
    }

    /**
     * 获取评论
     * @param articleId  博客id
     * @param limit       条数
     * @param timestamp    时间戳
     */
    private void getData(String articleId, int limit, int timestamp) {
        String url = "https://leancloud.cn:443/1.1/functions/getBlogComments";
        BlogCommentBean bean = new BlogCommentBean(articleId, limit, timestamp);
        Request request = HTTPUtils.requestPost(bean, url);
        OkHttpClient client = new OkHttpClient();
        client.newCall(request).enqueue(new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {

            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                String jsonResult = response.body().string();
                if(response.code()==200){
                BlogCommentResponse blogCommentResponse = new Gson().fromJson(jsonResult, BlogCommentResponse.class);
                mResultBeen.clear();
                mResultBeen.addAll(blogCommentResponse.getResult());

                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        mAdapter.notifyDataSetChanged();
                    }
                });

                }
            }
        });
    }
}
